package com.yu.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.task.mapper.TagMapper;
import com.yu.task.pojo.Tag;
import com.yu.task.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Autowired
    private TagMapper tagMapper;


    @Override
    public int insertTag(Tag tag) {
        try {
            QueryWrapper<Tag> qw = new QueryWrapper<>();
            qw.eq("name",tag.getName());
            if(tagMapper.selectOne(qw) == null ){
                int i = tagMapper.insert(tag);
                return 1;
            }else {
                return 2;
            }

        } catch (Exception e) {
            throw new RuntimeException(e);

        }
    }

    @Override
    public int updateTag(Tag tag) {
        try {
            int i = tagMapper.updateById(tag);
            if (i == 1) {
                return 1;
            }
            return 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteTag(Long id) {
        try {
            int i = tagMapper.deleteById(id);
            if (i == 1) {
                return 1;
            }
            return 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Tag> selectTag() {
        try {
            List<Tag> list = tagMapper.selectList(null);
            if (list == null) {
                return null;
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
